package org.afun.csms_android.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.afun.csms_android.R;
import org.afun.csms_android.adapter.Teacher2CourseSelectAdapter;
import org.afun.csms_android.adapter.TeacherShowCourseAdapter;
import org.afun.csms_android.domain.Course;
import org.afun.csms_android.domain.Teacher;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;

public class TeacherShowCourse extends Fragment {
    private static Context context;
    private static RecyclerView recyclerView;
    private static TextView emptyText;
    private static Teacher teacher;

    private static List<Course> myCourseList;


    public TeacherShowCourse(Context context, Teacher teacher) {
        TeacherShowCourse.context = context;
        TeacherShowCourse.teacher = teacher;
    }

    private static void showMsg() {
        MyTableOperator myTableOperator = new MyTableOperator(context);
        TableOperator tableOperator = myTableOperator.getTableOperator();
        myCourseList = tableOperator.getMyCourse(teacher);
        if (myCourseList.size() == 0) {
            emptyText.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            emptyText.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            // 瀑布流
            StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(staggeredGridLayoutManager);
//            // 网格布局
//            GridLayoutManager gridLayoutManager = new GridLayoutManager(context,LinearLayoutManager.VERTICAL);
//            recyclerView.setLayoutManager(gridLayoutManager);

            TeacherShowCourseAdapter showCourseAdapter = new TeacherShowCourseAdapter(myCourseList, context);
            recyclerView.setAdapter(showCourseAdapter);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_teacher_select_course_operator, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        emptyText = (TextView) view.findViewById(R.id.empty_text);
        showMsg();
        return view;
    }


    public void onResume() {
        super.onResume();
        TeacherShowCourse.showMsg();
    }

    public static void update() {
        TeacherShowCourse.showMsg();
    }
}